<template>
  <div class="custom-tree-node">
    <span class="dataTree">{{ data.name }}</span>
    <span class="people">{{ data.manager }}</span>
    <el-dropdown :hide-on-click="false" @command="commandClick">
      <span class="el-dropdown-link">
        操作<i class="el-icon-arrow-down el-icon--right" />
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="add">添加子部门</el-dropdown-item>
        <el-dropdown-item
          v-if="!data.top"
          command="look"
        >查看部门</el-dropdown-item>
        <el-dropdown-item
          v-if="!data.top"
          command="del"
        >删除部门</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
import { companydepartmentDel } from '@/api/departments'
export default {
  props: {
    data: {
      // 接收的是属性数据结构中的每一项, 点击的话可以得到该项
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {}
  },
  methods: {
    commandClick(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('showDialog', this.data, 'add')
          console.log('添加')
          break
        case 'look':
          this.$bus.$emit('showDialog', this.data, 'edit')
          console.log('查看')
          break
        case 'del':
          this.$confirm(`确定要删除[${this.data.name}]吗?`, '提示')
            .then(async() => {
              await companydepartmentDel(this.data.id)
              this.$message.success('删除成功')
              this.$emit('getData')
            })
            .catch(() => {})
          console.log('删除')
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  font-size: 14px;
  .dataTree {
    flex: 1;
  }
  .people {
    width: 100px;
    text-align: right;
    margin-right: 15px;
  }
}
</style>
